package com.fs.web.yz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.pojo.CheckNote;
import com.fs.common.pojo.CheckRecipes;
import com.fs.common.utils.ConstitutionAlgorithmUtils;
import com.fs.common.yzMapper.CheckRecipesMapper;
import com.fs.web.yz.service.ICheckRecipesService;
import com.fs.web.yz.utils.RecipesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CheckRecipesServiceImpl extends ServiceImpl<CheckRecipesMapper, CheckRecipes>
    implements ICheckRecipesService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public void createCheckRecipesForCheck(CheckNote cn) {
        if (cn != null){
            QueryWrapper<CheckRecipes> qw = new QueryWrapper<>();
            qw.eq("check_id", cn.getCheckId());
            int cnt = this.count(qw);
            //没有生成过菜谱的检查记录允许生成对应菜谱
            if (cnt == 0) {
                if (log.isInfoEnabled()){
                    log.info("checkNote(" + cn.getCheckId() + ") start create recipes!");
                }
                RecipesUtils ru = new RecipesUtils();

                JSONObject score = JSONObject.parseObject(cn.getCheckScore());
                //体质
                String resultContent = score.getString("resultContent");
                String[] constitutionArr = resultContent.split("、");
                //分娩日期
                Date deliveryDay = cn.getDeliveryDay();
                //分娩方式
                Integer deliveryType = cn.getDeliveryType();
                //喂养方式
                Integer feedType = cn.getFeedType();
                //忌口
                String avoidFood = cn.getAvoidFood();
                String[] avoidFoodArr = avoidFood.split(",");
                //获取症状
                String words = cn.getCheckWords();
                String[] symptomArr = words.split("#");
                //问卷症状
                ArrayList<String> symptomList = new ArrayList<String>(8);
                for (String symptom : symptomArr) {
                    if (symptom.matches(ConstitutionAlgorithmUtils.SymptomPattern)) {
                        symptomList.add(symptom);
                    }
                }

                //生成每天（1-28）的菜谱
                for (int begin = RecipesUtils.Month_First, end = RecipesUtils.Month_Last + 1; begin < end; begin++) {
                    //记录当天已选的菜谱，不分餐次菜品
                    List<CheckRecipes> chooseRecipes = ru.getDayCheckRecipes(cn, begin, deliveryDay,
                            deliveryType, feedType, symptomList, avoidFoodArr, constitutionArr);

                    //保存菜谱
                    saveBatch(chooseRecipes);
                    chooseRecipes = null;
                }
                if (log.isInfoEnabled()){
                    log.info("checkNote(" + cn.getCheckId() + ") finish create recipes!");
                }
                //判断菜谱是否已经生成
                cnt = this.count(qw);
                if (cnt > 0) {
                    cn.setCheckStatus(CheckNote.FINISH);
                    cn.updateById();
                }
            }else{
                if (log.isWarnEnabled()) {
                    log.warn("checkNote(" + cn.getCheckId() + ") has created recipes!");
                }
            }
        }
    }
}
